import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)

const routes = [{
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: () => import( /*webpackChunkName: "Login_Home" */ '@/components/Login.vue')
  },
  {
    path: '/home',
    component: () => import( /*webpackChunkName: "Login_Home" */ '@/components/Home.vue'),
    children: [{
        path: '/users',
        component: () => import( /*webpackChunkName: "Users" */ '@/views/Users.vue')
      },
      {
        path: '/roles',
        component: () => import( /*webpackChunkName: "Roles_Rights" */ '@/views/Roles.vue')
      },
      {
        path: '/rights',
        component: () => import( /*webpackChunkName: "Roles_Rights" */ '@/views/Rights.vue')
      },
      {
        path: '/goods',
        component: () => import( /*webpackChunkName: "Goods_AddGoods_Params_Categories" */ '@/views/Goods.vue')
      },
      {
        path: '/addgoods',
        component: () => import( /*webpackChunkName: "Goods_AddGoods_Params_Categories" */ '@/views/AddGoods.vue')
      },
      {
        path: '/params',
        component: () => import( /*webpackChunkName: "Goods_AddGoods_Params_Categories" */ '@/views/Params.vue')
      },
      {
        path: '/categories',
        component: () => import( /*webpackChunkName: "Goods_AddGoods_Params_Categories" */ '@/views/Categories.vue')
      },
      {
        path: '/orders',
        component: () => import( /*webpackChunkName: "Orders" */ '@/views/Orders.vue')
      },
      {
        path: '/reports',
        component: () => import( /*webpackChunkName: "Reports" */ '@/views/Reports.vue')
      },


    ]
  }

]

const router = new VueRouter({
  routes
})



router.beforeEach((to, from, next) => {
  // console.log(to);

  // if(to.matched.some(res=>res.meta.isLogin)){//判断是否需要登录
  //     if (sessionStorage['username']) {
  //         next();
  //     }else{
  //         next({
  //             path:"/login",
  //             query:{
  //                 redirect:to.fullPath
  //             }
  //         });
  //     }

  // }else{
  //     next()
  // }

  if (to.path == '/login') return next()

  var token = window.sessionStorage.getItem('token')
  // console.log(token);

  if (!token) return next('/login')

  next()



});


const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

export default router
